<!--
 * @Description: 心理评估--预览页
 * @Autor: -
 * @Date: 2021-07-11 13:28:19
 * @LastEditors: luojie
 * @LastEditTime: 2021-07-28 16:12:37
-->

<template>
  <view class="content myform">
    <uni-forms
      :value="formData"
      ref="form"
      validate-trigger="bind"
      err-show-type="undertext"
    >
      <view class="max-title" style="padding: 0"></view>
      <view class="info">
        <view class="info-item">
          <view>姓名</view>
          <view>{{ personInfo.name || "" }}</view>
        </view>
        <view class="info-item">
          <view>证件类型</view>
          <view>{{ personInfo.cardType || "" }}</view>
        </view>
        <view class="info-item">
          <view>性别</view>
          <view>{{ gender }}</view>
        </view>
      </view>

      <view class="max-title">亲友联系人</view>
      <view class="info">
        <view class="info-item info-item-watch">
          <view>联系人姓名</view>
          <view>{{ formData.friendsName }}</view>
        </view>
        <view class="info-item info-item-watch">
          <view>联系人电话</view>
          <view>{{ formData.friendsPhone }}</view>
        </view>
      </view>

      <view class="max-title" style="border: 0">既往病史</view>
      <view class="info">
        <view v-for="(item, index) in healthStatus" :key="index">
          <view class="info-item info-item-watch">
            <view>{{ item.label }}</view>
            <view>{{ item.value | valueData }}</view>
          </view>
          <view v-if="item.value === 1" class="info-item-desc">{{
            item.answer
          }}</view>
        </view>
        <view v-for="(item, index) in otherFormData" :key="index">
          <view class="info-item info-item-watch">
            <view>{{ item.label }}</view>
            <view>{{ item.text?item.text:'' }}</view>
          </view>
        </view>
      </view>

      <view class="max-title" style="border: none">评估分数</view>
      <view class="info last-info">
        <view class="info-item info-item-watch">
          <view>ISI
            <span v-show="resultList.scoreIsi | scorePD">得分：</span>
            <text style="color: #2c9af1; margin-left: 20rpx">
              {{ resultList.scoreIsi | scorePD }}
            </text>
          </view>
          <view>{{ resultList.resultIsi | resultPD }}</view>
        </view>
        <view class="info-item info-item-watch">
          <view>PHQ-9
            <span v-show="resultList.scorePhq9 | scorePD">得分：</span>
            <text style="color: #2c9af1; margin-left: 20rpx">
              {{ resultList.scorePhq9 | scorePD }}
            </text>
          </view>
          <view>{{ resultList.resultPhq9 | resultPD }}</view>
        </view>
        <view class="info-item info-item-watch">
          <view>GAD-7
            <span v-show="resultList.scoreGad7 | scorePD">得分：</span>
            <text style="color: #2c9af1; margin-left: 20rpx">
              {{ resultList.scoreGad7 | scorePD }}
            </text>
          </view>
          <view>{{ resultList.resultGad7 | resultPD }}</view>
        </view>
      </view>
      <view class="footer" v-if="!onlyRead">
        <button class="cancle" @click="goEdit">修改</button>
        <button class="dialogOk" @click="submitAll" v-if="canEdit != 1">确认</button>
      </view>
      <view class="single-footer" v-if="onlyRead">
        <button class="cancle" @click="goBack">返回</button>
      </view>
    </uni-forms>
    <view style="height: "></view>
  </view>
</template>

<script>
import { getData } from "../../api/index";
import { comprehensiveCong as formCong } from "./formConfig";
import { cardTypes } from "@/data";
import { translateDic } from "@/utils";

export default {
  data() {
    return {
      isChange: '0',
      sid:'',
      name: "",
      identity: "",
      nation: "",
      age: "",
      phone: "",
      isolationPoint: "",
      personInfo: {},
      sex: "0",
      rzfh: "--",
      resultList: {},
      formData: {
        friendsName: "",
        friendsPhone: "",
      },
      healthStatus: formCong.healthStatus,
      otherFormData: [
        {
          label: "对医学观察的态度",
          fieId: "attitudeTowardsMedicalObservation",
          value: "",
          text: "",
        },
        {
          label: "自伤自杀风险",
          fieId: "degreeSuicideAndSelfInjury",
          value: "",
          text: "",
        },
        {
          label: "心理现状",
          fieId: "statusQuoinHeart",
          value: "",
          text: "",
        },
        {
          label: "综合评估风险等级",
          fieId: "assessmentRiskLevel",
          value: "",
          text: "",
        },
        {
          label: "评估医生",
          fieId: "assessmentDoctor",
          value: "",
          text: "",
        },
        {
          label: "评估时间",
          fieId: "assessmentTime",
          value: "",
          text: "",
        },
      ],
      observationArr: [],
      injuryArr: [],
      comprehensiveArr: [],
      onlyRead: "",
      canEdit: "",
    };
  },
  filters: {
    sexPD: (value) => {
      if (value == 0) {
        return "女";
      } else if (value == 1) {
        return "男";
      } else {
        return "";
      }
    },
    scorePD: (value) => {
      if (value == null) {
        return "";
      } else {
        return value + '';
      }
    },
    resultPD: (value) => {
      if (value == null) {
        return "未评估";
      } else {
        return value;
      }
    },
    valueData: (value) => {
      if (value == 0) {
        return "否";
      } else if (value == 1) {
        return "是";
      } else {
        return "无";
      }
    },
  },
  async onLoad(option) {
    this.isChange = option.isChange;
    uni.showLoading();
    this.onlyRead = option.onlyRead;
    this.canEdit = option.canEdit;
    this.getSID();
    this.observationArr = await this.$store.dispatch('getDic','attitudeTowardsMedicalObservation');
    this.injuryArr = await this.$store.dispatch('getDic','degreeSuicideAndSelfInjury');
    this.comprehensiveArr = await this.$store.dispatch('getDic','assessment_riskLevel');
    let detailLits = uni.getStorageSync("detailLits");
    if (detailLits && detailLits.length > 0) {
      let otherFormData = {
        attitudeTowardsMedicalObservation: uni.getStorageSync(
          "attitudeTowardsMedicalObservation"
        ),
        degreeSuicideAndSelfInjury: uni.getStorageSync(
          "degreeSuicideAndSelfInjury"
        ),
        statusQuoinHeart: uni.getStorageSync("statusQuoinHeart"),
        assessmentRiskLevel: uni.getStorageSync("assessmentRiskLevel"),
        assessmentDoctor: uni.getStorageSync("assessmentDoctor"),
        assessmentTime: uni.getStorageSync("assessmentTime"),
      };
      this.otherFormData.map((item) => {
        // 对医学观察的态度
        if (item.fieId == "attitudeTowardsMedicalObservation") {
          this.observationArr.forEach((obj) => {
            if (obj.value == otherFormData[item.fieId]) {
              item.text = obj.label;
              item.value = otherFormData[item.fieId];
            }
          });
        } else if (item.fieId == "degreeSuicideAndSelfInjury") {
          // 自伤自杀风险
          this.injuryArr.forEach((obj) => {
            if (obj.value == otherFormData[item.fieId]) {
              item.text = obj.label;
              item.value = otherFormData[item.fieId];
            }
          });
        } else if (item.fieId == "assessmentRiskLevel") {
          // 综合评估风险等级
          this.comprehensiveArr.forEach((obj) => {
            if (obj.value == otherFormData[item.fieId]) {
              item.text = obj.label;
              item.value = otherFormData[item.fieId];
            }
          });
        } else {
          item.value = otherFormData[item.fieId];
          item.text = otherFormData[item.fieId];
        }
      });

      this.healthStatus = detailLits;
      this.formData = {
        friendsName: uni.getStorageSync("friendsName"),
        friendsPhone: uni.getStorageSync("friendsPhone"),
      };
      let results = uni.getStorageSync('results');
      this.resultList = results || {};
    } else {
      this.getList();
    }
    this.personInfo = uni.getStorageSync("personInfo");
    this.personInfo.cardType = translateDic(
      cardTypes,
      this.personInfo.cardType
    );
    uni.hideLoading();
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    submitAll() {
      const that = this;
      let healthStatusData = uni.getStorageSync("healthStatusData");

      let { personId, sid } = uni.getStorageSync("personInfo") //获取personId
      let params = {
        personId,
        contactName: this.formData.friendsName,
        contactPhone: this.formData.friendsPhone,
      }
      this.otherFormData.forEach(item => {
        params[item.fieId] = item.value;
      })

      this.healthStatus.forEach(item => {
        params[item.fieId] = item.value;
        params[item.inputFieId] = item.inputValue;
      })
      healthStatusData.sid = this.sid || sid;
      params.sid = this.sid || sid;
      const timer = this.getNowTime()
      healthStatusData.assessmentTime = "" + timer;
      params.assessmentTime = "" + timer;

      let method = 'POST';
      if (this.isChange == '1') {
        method = 'PUT';
      }

      getData("comprehensive", method, healthStatusData || params)
        .then((res) => {
          uni.navigateTo({
            url: `./success?title=psychological&isChange=${that.isChange}`,
          });
          uni.removeStorageSync('isolationPersonnel');
          uni.removeStorageSync('healthScreenData');
        })
        .catch((e) => {
          console.log(e);
        });
    },
    getNowTime: function () {
      let dateTime
      let yy = new Date().getFullYear()
      let mm = new Date().getMonth() + 1
      let dd = new Date().getDate()
      let hh = new Date().getHours()
      mm = mm<10?'0'+mm:mm;
      dd = dd<10?'0'+dd:dd;
      hh = hh<10?'0'+hh:hh;
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes()
        :
        new Date().getMinutes()
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds()
        :
        new Date().getSeconds()
      dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
     // console.log(dateTime)
      return dateTime
    },
    getSID(){
      let { personId } = uni.getStorageSync("personInfo");
      let parms = { personId };
      getData("comprehensive", "get", parms)
        .then((res) => {
          this.sid = res[0].sid;
        })
    },
    getList() {
      let { personId } = uni.getStorageSync("personInfo");
      let parms = { personId };
      getData("comprehensive", "get", parms)
        .then((res) => {
          if (res != null && res != "") {
            this.resultList = res[0];
            this.healthStatus.forEach((item) => {
              item.value = this.resultList[item.fieId];
              item.answer = this.resultList[item.inputFieId];
              if (item.fieId == "otherFieId") {
                item.answer = this.resultList.otherSupplement;
                item.value = this.resultList.otherSupplement ? 1 : 0;
              }
              this.formData.friendsName = this.resultList.contactName || "";
              this.formData.friendsPhone = this.resultList.contactPhone || "";
            });

            this.otherFormData.forEach((item) => {
              // 对医学观察的态度
              if (item.fieId == "attitudeTowardsMedicalObservation") {
                this.observationArr.forEach((obj) => {
                  if (obj.value == this.resultList[item.fieId]) {
                    item.text = obj.label;
                    item.value = this.resultList[item.fieId];
                  }
                });
              } else if (item.fieId == "degreeSuicideAndSelfInjury") {
                // 自伤自杀风险
                this.injuryArr.forEach((obj) => {
                  if (obj.value == this.resultList[item.fieId]) {
                    item.text = obj.label;
                    item.value = this.resultList[item.fieId];
                  }
                });
              } else if (item.fieId == "assessmentRiskLevel") {
                // 综合评估风险等级
                this.comprehensiveArr.forEach((obj) => {
                  if (obj.value == this.resultList[item.fieId]) {
                    item.text = obj.label;
                    item.value = this.resultList[item.fieId];
                  }
                });
              } else {
                item.value = this.resultList[item.fieId];
                item.text = this.resultList[item.fieId];
                // console.log(item)
              }
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    goEdit() {
      uni.navigateTo({
        url: `./comprehensive?isChange=${this.isChange}`,
      });
    }
  },
  computed:{
    gender:function(){
      // 先判断gender，若没有值则判断身份证倒数第二位，奇数为男，偶数为女
      if (this.personInfo.gender ==0) {
        return '女'
      }else if(this.personInfo.gender ==1){
        return '男'
      }else{
        if (this.personInfo.cardType == "身份证") {
          console.log('身份证倒数第二位：',this.personInfo.cardId.slice(-2,-1));
          if (this.personInfo.cardId && this.personInfo.cardId.slice(-2,-1)%2 == 0 ) {
            return '女'
          }else if(this.personInfo.cardId && this.personInfo.cardId.slice(-2,-1)%2 != 0){
            return '男'
          }else{
            return ''
          }
        }else{
          return ''
        }
      }
    }
  }
};
</script>

<style lang='scss' scrope>

.myform {
  background-color: #fff;
  padding-bottom: 0;
}
.content {
  display: flex;
  flex-direction: column;
  .uni-forms-item__label {
    width: 300rpx !important;
    .label-text {
      color: #333 !important;
    }
  }
  .uni-forms-item {
    padding: 0 40rpx !important;
  }
  .uni-forms-item__inner {
    padding-bottom: 0 !important;
  }
  .footer,
  .single-footer {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    justify-content: space-between;
    padding: 58rpx 32rpx;
    // border: 1rpx solid #cccccc;
    background: #fff;
    .cancle {
      width: 328rpx;
      color: #000000;
      font-size: 34rpx;
      background: #fff;
      border: 1rpx solid #cccccc;
      border-radius: 16rpx;
      margin-right: 30rpx;
    }
    .dialogOk {
      width: 328rpx;
      color: #fff;
      font-size: 34rpx;
      background: #2c9af1;
      border-radius: 16rpx;
    }
  }
  .single-footer {
    width: 100%;
    justify-content: center;
    align-items: center;
    .cancle {
      margin: 0 auto;
    }
  }
}
.title {
  font-size: 40rpx;
  color: #000000;
  font-weight: bold;
}
.max-title {
  font-size: 40rpx;
  color: #000000;
  font-weight: 600;
  border-top: 60rpx solid #f9f9f9;
  padding: 30rpx 40rpx;
  box-sizing: border-box;
  .default-all {
    flex: 1;
    padding-left: 40px;
    color: #53adf3;
    font-size: 30rpx;
    line-height: 70rpx;
  }
}
.info {
  font-size: 30rpx;
  padding: 0 40rpx;
  &.last-info {
    padding: 0 40rpx 180rpx;
  }
  &-item {
    display: flex;
    justify-content: space-between;
    padding: 18rpx 0;
    border-bottom: 1px solid #f9f9f9;
    font-weight: 500;
    font-size: 26rpx;
    &-watch {
      color: #666;
      border: none;
    }
    &:last-child {
      border-bottom: 0;
    }
    &-desc {
      color: #666;
      padding-left: 40rpx;
      box-sizing: border-box;
    }
  }
}
.lettle-title {
  font-size: 40rpx;
  font-weight: 600;
  line-height: 98rpx;
  height: 98rpx;
}

.input-style {
  font-size: 24rpx;
  margin-top: 20px;
  border-bottom: 1rpx solid #e4e4e4;
}
.flex-box {
  height: 100rpx;
  line-height: 100rpx;
  font-size: 26rpx;
  display: flex;
  padding: 0 40rpx;
  justify-content: space-between;
  color: #333333;
  text {
    font-size: 26rpx;
    color: #000;
  }
}
.question {
  .uni-forms-item__label {
    width: 300rpx !important;
    .label-text {
      color: #333 !important;
    }
  }
}
.btn-con {
  width: 100%;
  background-color: #f9f9f9;
  padding: 40rpx;
  box-sizing: border-box;
}
.btn {
  width: 686rpx;
  height: 94rpx;
  background: #2c9af1;
  border-radius: 16rpx;
}

// .item-jbxx {
//   /* display: flex;
//   justify-content: space-between;
//   height: 130rpx; */
// }
.small-title {
  font-size: 30rpx;
  font-family: PingFang SC;
  font-weight: bold;
  color: #000000;
  margin: 40rpx 0 0 0;
}
</style>
